package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Echart;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.*;

@Repository("EchartDAO")
@Transactional
public class EchartDAOImpl extends AbstractJpaDao<Echart> implements EchartDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Echart.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public EchartDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Echart findEchartById(Integer id) throws DataAccessException {

		return findEchartById(id, -1, -1);
	}

	@Transactional
	public Echart findEchartById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEchartById", startResult, maxRows, id);
			return (Echart) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Echart> findEchartByName(String name) throws DataAccessException {

		return findEchartByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findEchartByName", startResult, maxRows, name);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByNameContaining(String name) throws DataAccessException {

		return findEchartByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByEvent(Integer event) throws DataAccessException {

		return findEchartByEvent(event, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByEvent(Integer event, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findEchartByEvent", startResult, maxRows, event);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByEventname(String eventname) throws DataAccessException {

		return findEchartByEventname(eventname, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByEventname(String eventname, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByEventname", startResult, maxRows, eventname);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByEventnameContaining(String eventname) throws DataAccessException {

		return findEchartByEventnameContaining(eventname, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByEventnameContaining(String eventname, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByEventnameContaining", startResult, maxRows, eventname);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartBySourcecountry(String sourcecountry) throws DataAccessException {

		return findEchartBySourcecountry(sourcecountry, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartBySourcecountry(String sourcecountry, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartBySourcecountry", startResult, maxRows, sourcecountry);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartBySourcecountryContaining(String sourcecountry) throws DataAccessException {

		return findEchartBySourcecountryContaining(sourcecountry, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartBySourcecountryContaining(String sourcecountry, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartBySourcecountryContaining", startResult, maxRows, sourcecountry);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartBySourceprovince(String sourceprovince) throws DataAccessException {

		return findEchartBySourceprovince(sourceprovince, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartBySourceprovince(String sourceprovince, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartBySourceprovince", startResult, maxRows, sourceprovince);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartBySourceprovinceContaining(String sourceprovince) throws DataAccessException {

		return findEchartBySourceprovinceContaining(sourceprovince, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartBySourceprovinceContaining(String sourceprovince, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartBySourceprovinceContaining", startResult, maxRows, sourceprovince);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartBySourcecity(String sourcecity) throws DataAccessException {

		return findEchartBySourcecity(sourcecity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartBySourcecity(String sourcecity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartBySourcecity", startResult, maxRows, sourcecity);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartBySourcecityContaining(String sourcecity) throws DataAccessException {

		return findEchartBySourcecityContaining(sourcecity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartBySourcecityContaining(String sourcecity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartBySourcecityContaining", startResult, maxRows, sourcecity);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByDiscoverycountry(String discoverycountry) throws DataAccessException {

		return findEchartByDiscoverycountry(discoverycountry, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByDiscoverycountry(String discoverycountry, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByDiscoverycountry", startResult, maxRows, discoverycountry);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByDiscoverycountryContaining(String discoverycountry) throws DataAccessException {

		return findEchartByDiscoverycountryContaining(discoverycountry, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByDiscoverycountryContaining(String discoverycountry, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByDiscoverycountryContaining", startResult, maxRows,
				discoverycountry);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByDiscoveryprovince(String discoveryprovince) throws DataAccessException {

		return findEchartByDiscoveryprovince(discoveryprovince, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByDiscoveryprovince(String discoveryprovince, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByDiscoveryprovince", startResult, maxRows, discoveryprovince);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByDiscoveryprovinceContaining(String discoveryprovince) throws DataAccessException {

		return findEchartByDiscoveryprovinceContaining(discoveryprovince, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByDiscoveryprovinceContaining(String discoveryprovince, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByDiscoveryprovinceContaining", startResult, maxRows,
				discoveryprovince);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByDiscoverycity(String discoverycity) throws DataAccessException {

		return findEchartByDiscoverycity(discoverycity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByDiscoverycity(String discoverycity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByDiscoverycity", startResult, maxRows, discoverycity);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByDiscoverycityContaining(String discoverycity) throws DataAccessException {

		return findEchartByDiscoverycityContaining(discoverycity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByDiscoverycityContaining(String discoverycity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByDiscoverycityContaining", startResult, maxRows, discoverycity);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByFoodtypename(String foodtypename) throws DataAccessException {

		return findEchartByFoodtypename(foodtypename, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByFoodtypename(String foodtypename, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByFoodtypename", startResult, maxRows, foodtypename);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByFoodtypenameContaining(String foodtypename) throws DataAccessException {

		return findEchartByFoodtypenameContaining(foodtypename, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByFoodtypenameContaining(String foodtypename, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByFoodtypenameContaining", startResult, maxRows, foodtypename);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByFoodtype(Integer foodtype) throws DataAccessException {

		return findEchartByFoodtype(foodtype, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByFoodtype(Integer foodtype, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findEchartByFoodtype", startResult, maxRows, foodtype);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartBySourceTime(Calendar sourceTime) throws DataAccessException {

		return findEchartBySourceTime(sourceTime, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartBySourceTime(Calendar sourceTime, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartBySourceTime", startResult, maxRows, sourceTime);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByDiscoveryTime(Calendar discoveryTime) throws DataAccessException {

		return findEchartByDiscoveryTime(discoveryTime, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByDiscoveryTime(Calendar discoveryTime, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByDiscoveryTime", startResult, maxRows, discoveryTime);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByDescription(String description) throws DataAccessException {

		return findEchartByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByDescriptionContaining(String description) throws DataAccessException {

		return findEchartByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByAdultertype(String adultertype) throws DataAccessException {

		return findEchartByAdultertype(adultertype, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByAdultertype(String adultertype, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByAdultertype", startResult, maxRows, adultertype);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByAdultertypeContaining(String adultertype) throws DataAccessException {

		return findEchartByAdultertypeContaining(adultertype, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByAdultertypeContaining(String adultertype, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByAdultertypeContaining", startResult, maxRows, adultertype);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByAdulterlink(String adulterlink) throws DataAccessException {

		return findEchartByAdulterlink(adulterlink, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByAdulterlink(String adulterlink, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByAdulterlink", startResult, maxRows, adulterlink);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByAdulterlinkContaining(String adulterlink) throws DataAccessException {

		return findEchartByAdulterlinkContaining(adulterlink, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByAdulterlinkContaining(String adulterlink, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByAdulterlinkContaining", startResult, maxRows, adulterlink);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByFoodname(String foodname) throws DataAccessException {

		return findEchartByFoodname(foodname, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByFoodname(String foodname, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findEchartByFoodname", startResult, maxRows, foodname);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByFoodnameContaining(String foodname) throws DataAccessException {

		return findEchartByFoodnameContaining(foodname, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByFoodnameContaining(String foodname, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByFoodnameContaining", startResult, maxRows, foodname);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartBySubject(String subject) throws DataAccessException {

		return findEchartBySubject(subject, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartBySubject(String subject, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findEchartBySubject", startResult, maxRows, subject);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartBySubjectContaining(String subject) throws DataAccessException {

		return findEchartBySubjectContaining(subject, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartBySubjectContaining(String subject, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartBySubjectContaining", startResult, maxRows, subject);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByFoodmaintype(String foodmaintype) throws DataAccessException {

		return findEchartByFoodmaintype(foodmaintype, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByFoodmaintype(String foodmaintype, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByFoodmaintype", startResult, maxRows, foodmaintype);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByFoodmaintypeContaining(String foodmaintype) throws DataAccessException {

		return findEchartByFoodmaintypeContaining(foodmaintype, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByFoodmaintypeContaining(String foodmaintype, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByFoodmaintypeContaining", startResult, maxRows, foodmaintype);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByFoodbatchfiller(String foodbatchfiller) throws DataAccessException {

		return findEchartByFoodbatchfiller(foodbatchfiller, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByFoodbatchfiller(String foodbatchfiller, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByFoodbatchfiller", startResult, maxRows, foodbatchfiller);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByFoodbatchfillerContaining(String foodbatchfiller) throws DataAccessException {

		return findEchartByFoodbatchfillerContaining(foodbatchfiller, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByFoodbatchfillerContaining(String foodbatchfiller, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByFoodbatchfillerContaining", startResult, maxRows, foodbatchfiller);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByPunish(String punish) throws DataAccessException {

		return findEchartByPunish(punish, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByPunish(String punish, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findEchartByPunish", startResult, maxRows, punish);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Set<Echart> findEchartByPunishContaining(String punish) throws DataAccessException {

		return findEchartByPunishContaining(punish, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByPunishContaining(String punish, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEchartByPunishContaining", startResult, maxRows, punish);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@Transactional
	public Echart findEchartByPrimaryKey(Integer id) throws DataAccessException {
		return findEchartByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Echart findEchartByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEchartByPrimaryKey", startResult, maxRows, id);
			return (Echart) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Echart> findEchartsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEchartsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Echart> findAllEcharts() throws DataAccessException {

		return findAllEcharts(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findAllEcharts(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllEcharts", startResult, maxRows);
		return new LinkedHashSet<Echart>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Echart> findEchartByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Echart>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Echart> findEchartsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Echart.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Echart> echarts = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return echarts;
	}

	@Transactional
	public Echart findEchartByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findEchartByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Echart findEchartByPhysicalPrimaryKey(String name, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEchartByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Echart) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Echart entity) {
		return true;
	}
}
